#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/3/6 15:54
@desc
"""

import time

import sqlalchemy.exc
from sqlalchemy import create_engine, MetaData, Table, Column, String, Float, Text, BigInteger
from sqlalchemy.engine.url import make_url
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy.pool import NullPool
from sqlalchemy.ext.declarative import declarative_base
from pyspider.database.base.projectdb import ProjectDB as BaseProjectDB

BaseModel = declarative_base()

class PartnerCode(BaseModel):
    __tablename__ = 'PartnerCode'

    def __init__(self, url):
        self.table = Table(self.__tablename__, MetaData(),
                           Column('ID', String(32)),
                           Column('ETag', BigInteger),
                           Column('Type', String(16)),
                           mysql_engine='InnoDB',
                           mysql_charset='utf8'
                           )
        engine = create_engine(url, pool_size=10, max_overflow=10, pool_timeout=30, pool_recycle=5)
        SessionFactory = sessionmaker(bind=engine)
        session = scoped_session(SessionFactory)
        self.engine = engine
        self.session = session
        self.table.create(self.engine, checkfirst=True)

    @staticmethod
    def _stringify(data):
        return data

    def insert(self, obj={}):

        return self.session.add(obj)


if __name__ == '__main__':
    url = 'mysql+pymysql://root:newpasswd@192.168.100.161:3306/OLINGO'
    partnerCode = PartnerCode(url)
    data = {'ID': '12345', 'ETag': 191, 'Type': '9'}
    partnerCode.insert(data)

    bk = input('>>>')

